import {
  createRouter,
  createWebHistory
} from 'vue-router'
import {
  ElMessage
} from 'element-plus'
import Login from '../views/Login.vue'
import Register from '../views/Register.vue'
import Index from '../views/Index.vue'
import Appointment from '../views/Appointment.vue'
import ReportList from '../views/ReportList.vue'
import Personal from '../views/Personal.vue'
import Hospital from '../views/Hospital.vue'
import Setmeal from '../views/Setmeal.vue'
import SelectDate from '../views/SelectDate.vue'
import ConfirmOrder from '../views/ConfirmOrder.vue'
import AppointmentSuccess from '../views/AppointmentSuccess.vue'
import AppointmentCancel from '../views/AppointmentCancel.vue'
import AppointmentList from '../views/AppointmentList.vue'
import Report from '../views/Report.vue'
import AskEveryone from '../views/AskEveryone.vue'
import CommentList   from "../views/CommentList.vue";
import AIChat from "../views/AIChat.vue";
const router = createRouter({
  history: createWebHistory(
    import.meta.env.BASE_URL),
  routes: [{
    path: '/',
    name: 'home',
    component: Login
  }, {
    path: '/login',
    name: 'login',
    component: Login
  }, {
    path: '/register',
    name: 'register',
    component: Register
  }, {
    path: '/index',
    name: 'index',
    component: Index
  }, {
    path: '/appointment',
    name: 'appointment',
    component: Appointment
  }, {
    path: '/reportList',
    name: 'reportList',
    component: ReportList
  }, {
    path: '/personal',
    name: 'personal',
    component: Personal
  }, {
    path: '/hospital',
    name: 'hospital',
    component: Hospital
  }, {
    path: '/setmeal',
    name: 'setmeal',
    component: Setmeal
  }, {
    path: '/selectDate',
    name: 'selectDate',
    component: SelectDate
  }, {
    path: '/confirmOrder',
    name: 'confirmOrder',
    component: ConfirmOrder
  }, {
    path: '/appointmentSuccess',
    name: 'appointmentSuccess',
    component: AppointmentSuccess
  }, {
    path: '/appointmentCancel',
    name: 'appointmentCancel',
    component: AppointmentCancel
  }, {
    path: '/appointmentList',
    name: 'appointmentList',
    component: AppointmentList
  }, {
    path: '/report',
    name: 'report',
    component: Report
  }, {
    path: '/askEveryone',
    name: 'askEveryone',
    component: AskEveryone
  },
    {
      path: '/commentList',
      name: 'commentList',
      component: CommentList
    },
    {
      path: '/AIChat',
      name: 'AIChat',
      component: AIChat
    },]

})

// 全局导航守卫
router.beforeEach((to, from, next) => {
  const isAuthenticated = !!sessionStorage.getItem('token'); // 检查用户是否已登录
  if (to.path !== '/login' && to.path !== '/register' && !isAuthenticated) {
    ElMessage({
      type: 'error',
      message: '您还未登录,请先登录!',
    })
    next('/login'); // 如果未登录且不是跳转到登录页，则跳转到登录页
  } else {
    next(); // 已登录或者访问登录页，直接通过
  }
});

export default router